#include <stdio.h>
#include <stdlib.h>


typedef struct linknode
{
    int data;
    struct linknode *next;
}link_node;

link_node *creat()
{
    link_node *H=(link_node *)malloc(sizeof(link_node));
    H->data=0;
    H->next=NULL;

    return H;
}                                                                                                                                                                                                                                             

int insert(link_node *H,int mydata)
{
    link_node *p=(link_node *)malloc(sizeof(link_node));
    link_node *q=H;
    while(H->next!=NULL)
    {
        H=H->next;
    }
    p->next=NULL;
    H->next=p;
    p->data=mydata;
    q->data++;

    return 0;
}

int view(link_node *H)
{
    link_node *p=H;
    while(p->next!=NULL)
    {
        p=p->next;
        printf("%d ",p->data);

    }
    putchar(10);

    return 0;
}

link_node *linkopera(link_node *H,int num)
{
	link_node *h=(link_node *)malloc(sizeof(link_node));
	h->data=0;
	h->next=NULL;
	link_node *b=h;
	link_node *p=H;
	link_node *a=NULL;
	while(p->next!=NULL)
	{
		if(num>p->next->data)
		{
			a=p->next;
			p->next=a->next;
			a->next=NULL;
			b->next=a;
			b=b->next;
		}
		else
		{
			p=p->next;
		}
	}
	b->next=H->next;
	free(H);

	return h;
}

int main(int argc, const char *argv[])
{
	link_node *H=creat();
	insert(H,5);
	insert(H,6);
	insert(H,4);
	insert(H,3);
	insert(H,1);
	insert(H,2);
	view(H);
	int num=0;
	scanf("%d",&num);
	link_node *h=linkopera(H,num);
	view(h);

	return 0;
}
